package top.whysu.rongcloud.controller;

import io.rong.RongCloud;
import io.rong.models.Result;
import io.rong.models.response.ResponseResult;
import io.rong.models.sensitiveword.SensitiveWordModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.whysu.rongcloud.constant.ConfigConstant;


@RestController
@RequestMapping("/rongCloud/sensitiveword")
public class SensitiveWordController {

    private RongCloud rongCloud = RongCloud.getInstance(ConfigConstant.appKey, ConfigConstant.appSecret);

    //添加敏感词服务
    @GetMapping("/add")
    Result add(SensitiveWordModel sensitiveWordModel) {
        Result result = null;
        try {
            result = rongCloud.sensitiveword.add(sensitiveWordModel);

        } catch (Exception e) {

            return new ResponseResult(500, "服务器内部错误,抛出异常");
        }

        return result;
    }

    //移除敏感词服务
    @GetMapping("/delete")
    Result delete(@RequestParam("word") String word) {
        Result result = null;
        try {
            result = rongCloud.sensitiveword.remove(word);

        } catch (Exception e) {

            return new ResponseResult(500, "服务器内部错误,抛出异常");
        }

        return result;
    }

    //批量移除敏感词服务
    @GetMapping("/batch/delete")
    Result batch_delete(@RequestParam("words") String[] words) {
        Result result = null;
        try {
            result = rongCloud.sensitiveword.batchDelete(words);

        } catch (Exception e) {

            return new ResponseResult(500, "服务器内部错误,抛出异常");
        }

        return result;
    }

    //查询敏感词列表方法
    @GetMapping("/list")
    Result list(@RequestParam(value = "type", required = false) String type) {
        if (StringUtils.isEmpty(type)) {
            type = "1";
        }

        Result result = null;
        try {
            result = rongCloud.sensitiveword.getList(Integer.parseInt(type));

        } catch (Exception e) {

            return new ResponseResult(500, "服务器内部错误,抛出异常");
        }

        return result;
    }
}
